package com.pi.shisan.service.service.impl.next;

import com.pi.shisan.service.annotation.CalcNextTimeType;
import com.pi.shisan.service.constants.ClockTypeEnum;
import com.pi.shisan.service.dto.ClockInfoDTO;
import com.pi.shisan.service.dto.NextVacationDTO;
import com.pi.shisan.service.service.ClockNextTimeService;
import com.pi.shisan.service.service.GovHolidayService;
import org.springframework.beans.factory.annotation.Autowired;

import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Collections;
import java.util.List;
import java.util.Objects;

@CalcNextTimeType(ClockTypeEnum.CHINA_VACATION)
public class ChinaVacationNextTimeServiceImpl implements ClockNextTimeService {

    @Autowired
    private GovHolidayService govHolidayService;

    @Override
    public Long calc(ClockInfoDTO clockInfoDTO) {
        LocalDateTime now = LocalDateTime.now();
        LocalDate date = now.toLocalDate();
        if (now.getHour() > clockInfoDTO.getClkHour() || (Objects.equals(now.getHour() , clockInfoDTO.getClkHour()) && now.getMinute() >= clockInfoDTO.getClkMinute())) {
            date = date.plusDays(1);
        }
        List<NextVacationDTO> nextVacationDTOS = null;
        List<NextVacationDTO> nextOfficeDTOS = null;
        there: while (true) {
            int currDayint = date.getYear() * 10000 + date.getMonthValue() * 100 + date.getDayOfMonth();
            if (date.getDayOfWeek() == DayOfWeek.SATURDAY || date.getDayOfWeek() == DayOfWeek.SUNDAY) {
                if (nextOfficeDTOS == null) {
                    nextOfficeDTOS = govHolidayService.allNextOffice(date);
                    if (nextOfficeDTOS == null) {
                        nextOfficeDTOS = Collections.EMPTY_LIST;
                    }
                    if (nextOfficeDTOS.isEmpty()) {
                        break;
                    }
                }
                for (NextVacationDTO v : nextOfficeDTOS) {
                    if (v.getDayint() > currDayint) {
                        break there;
                    } else if (v.getDayint() == currDayint) {
                        date = date.plusDays(1);
                        continue there;
                    }
                }
                break;
            } else {
                if (nextVacationDTOS == null) {
                    nextVacationDTOS = govHolidayService.allNextVacation(date);
                    if (nextVacationDTOS == null) {
                        nextVacationDTOS = Collections.EMPTY_LIST;
                    }
                }
                for (NextVacationDTO v : nextVacationDTOS) {
                    if (v.getDayint() > currDayint) {
                        break;
                    } else if (v.getDayint() == currDayint) {
                        clockInfoDTO.setHolidayName(v.getName());
                        break there;
                    }
                }
                date = date.plusDays(1);
            }
        }
        return LocalDateTime.of(date.getYear(), date.getMonthValue(), date.getDayOfMonth(), clockInfoDTO.getClkHour(), clockInfoDTO.getClkMinute(),0)
                .atZone(ZoneId.systemDefault()).toInstant().toEpochMilli();
    }
}
